package com.hzit.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.pojo.Classes;
import com.hzit.pojo.R;
import com.hzit.service.ClassesService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
	private ClassesService classesService;
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		classesService = wac.getBean(ClassesService.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf8");
		//2.1 得到请求参数
		String cmd = req.getParameter("cmd");
		//2.2 通过参数调用不同方法
		if(StrUtil.isNotBlank(cmd)){
			if ("list".equals(cmd)) {
				list(req,resp);
			}
		}
	}
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.3 查询所有班级
		List<Classes> classes = classesService.findAll();
		R r = R.ok().data("classes",classes);
		String s1 = JSON.toJSONString(r);
		resp.getWriter().println(s1);
		resp.getWriter().flush();
	}

}
